package learningjava;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

public class OracleCC {
   Connection ConnectC(String Path) throws java.sql.SQLException {     
        try
        {
         java.util.Locale.setDefault(java.util.Locale.ENGLISH);
         java.sql.DriverManager.registerDriver(new oracle.jdbc.OracleDriver());     
         return java.sql.DriverManager.getConnection(Path,"test","test");
        }
         catch (Exception e){e.toString();}
        return null;
}
 
   void ReadSQLtoDTM(String Path,String Query,DefaultTableModel dtm)
  {
        try
        {
         Connection con = ConnectC(Path);    
         Statement statement=con.createStatement();
        
         statement.executeQuery(Query);
        
         ResultSet rs=statement.getResultSet();
         ArrayList al = new ArrayList();
        // dtm.setColumnCount(0);
        
         
         for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
         dtm.addColumn(rs.getMetaData().getColumnName(i));
         //dtm.addColumn("rowid");
                 
         
         
         while(rs.next())
         {
           al.clear();  
           for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
               al.add(rs.getString(i));
           dtm.addRow(al.toArray());
         }
        
         statement.close();
         con.close();
       } 
        catch(Exception e){e.toString();}
  }
  void ReadSQLtoDCBM(String Path,String Query,DefaultComboBoxModel dcbm)
  {
      ArrayList al = new ArrayList();
        try
        {
         Connection con = ConnectC(Path);    
         Statement statement=con.createStatement();
        
         statement.executeQuery(Query);
        
         ResultSet rs=statement.getResultSet();
        
         while(rs.next())
           dcbm.addElement(rs.getString(1));
        
         statement.close();
         con.close();
       } 
        catch(Exception e){e.toString();}
  } 
  
  
  
   void ReadSQLtoDTM1(String Path,String Query,String Query1,DefaultTableModel dtm)
  {
        try
        {
         Connection con = ConnectC(Path);    
         Statement statement=con.createStatement();
        Statement statement1=con.createStatement();
        
         statement.executeQuery(Query);
        statement1.executeQuery(Query1);
        
         ResultSet rs=statement.getResultSet();
         ResultSet rs1=statement1.getResultSet();
         
         ArrayList al = new ArrayList();
        dtm.setColumnCount(0);
        
     //    dtm.addColumn("rowid");
         for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
         dtm.addColumn(rs.getMetaData().getColumnName(i));
        // dtm.addColumn("rowid");
                 
         
         
         while(rs.next() && rs1.next())
         {
           al.clear();  
           al.add(rs1.getString(1));
           for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
               al.add(rs.getString(i));
           
           dtm.addRow(al.toArray());
         }
        
         statement.close();
         con.close();
       } 
        catch(Exception e){e.toString();}
  }
}
